package com.avic.modules.storage.permit;

import com.avic.annotation.AnonymousAccess;
import com.avic.modules.quartz.utils.Csv2Excel;
import com.avic.modules.storage.utils.ResponseUtils;
import com.avic.tools.config.bean.AvicConfigElement;
import com.avic.utils.FileUtil;
import com.avic.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@RestController
@Api(tags = "系统：静态文件获取")
@RequestMapping("/static")
@ConditionalOnMissingBean(AvicConfigElement.class)
@Slf4j
public class StaticController {

    private final AvicConfigElement avicConfigElement;

    public StaticController(AvicConfigElement avicConfigElement) {
        this.avicConfigElement = avicConfigElement;
    }

    @ApiOperation("静态规则图片获取")
    @RequestMapping("/TERule/{fileName}")
    @AnonymousAccess
    public ResponseEntity<byte[]> getStaticFile(@PathVariable String fileName) throws IOException {
        if (StringUtils.isEmpty(fileName)) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        String fileAbsPath = avicConfigElement.getEquipTERuleLocation().concat("/" + fileName);
        return ResponseUtils.getResponseEntity(fileName, fileAbsPath, false);
    }

    @ApiOperation("静态头像获取，参数需要 Base64")
    @RequestMapping("/avatar/{pathName}")
    @AnonymousAccess
    public ResponseEntity<byte[]> getAvatar(@PathVariable String pathName) throws IOException {
        String base64DecodedPath = new String(Base64.decodeBase64(pathName), StandardCharsets.UTF_8);
        if (StringUtils.isEmpty(base64DecodedPath)) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        String fileName = FileUtil.getName(base64DecodedPath);
        return ResponseUtils.getResponseEntity(fileName, base64DecodedPath, false);
    }

    @ApiOperation("kkfileview 静态文件获取，参数需要")
    @GetMapping("/taskData/{newFileName}")
    @AnonymousAccess
    public ResponseEntity<byte[]> getReportData(@RequestParam String equipmentCode,@PathVariable String newFileName) throws IOException {
        String path = avicConfigElement.getLocation();
        String absPath = path + File.separator + equipmentCode + File.separator + newFileName;
        String fileName = FileUtil.getName(absPath);
        return ResponseUtils.getResponseEntity(fileName, absPath, false);
    }

    @ApiOperation("kkfileview 静态文件预览，参数需要 originName")
    @GetMapping("/preview/{originName}")
    @AnonymousAccess
    public ResponseEntity<byte[]> previewZhVersion(@RequestParam String equipmentCode,@PathVariable String originName, @RequestParam String newFileName) throws IOException, InterruptedException {
        String path = avicConfigElement.getLocation();
        String absPath = path + File.separator + equipmentCode + File.separator + newFileName;
        if(absPath.endsWith("csv") || absPath.endsWith("CSV")){
            absPath = Csv2Excel.convert(absPath);
            Thread.sleep(500);
        }
        return ResponseUtils.getResponseEntity(originName, absPath, false);
    }

}
